package org.example.com.cn.Business;

import org.example.com.cn.DAO.midtermDAO;
import org.example.com.cn.Model.midterm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScoreService {
   midtermDAO midtermDAO;

    @Autowired
    public void setMidtermDAO(midtermDAO midtermDAO) {
        this.midtermDAO = midtermDAO;
    }

    public float avarageScore() {
        List<midterm> students = midtermDAO.queryAll();
        float scores = 0;
        for(midterm s : students) {
            scores += s.getScore();
        }
        System.out.println(scores/students.size());
        return scores/students.size();
    }
    public void insertStudent(midterm student) {
        midtermDAO.insertMidterm(student);
    }

    @Transactional
    public void modifyScore() {
        List<midterm> students = midtermDAO.queryAll();
        for(midterm s : students) {
            int score = (int)s.getScore();
            int G = score/10;
            switch (G) {
                case 10:
                case 9:s.setScore(1f);break;
                case 8:s.setScore(2f);break;
                case 7:s.setScore(3f);break;
                case 6:s.setScore(4f);break;
                default:s.setScore(5f);
            }
            System.out.println(s.getScore());
            midtermDAO.modifyMidterm(s);
        }
    }



}
